<template>
  <el-button @click="selectFileTempList">{{ label }}<i class="el-icon-download el-icon--right"></i></el-button>
</template>

<script>
export default {

  props: {
    label: String,
    service: {
      type: String,
    },
    module: {
      type: String,
    },
  },
  data() {
    return {
      temps: [],
    }
  },
  methods: {
    loadFileTemp(call) {
      this.$ajax({
        url: this.$url['oms/baseTemplate'],
        method: 'post',
        loading: true,
        data: {
          service: this.service,
          module: this.module,
        },
        success: (res) => {
          this.temps = res.data.rows
          call()
        }
      })
    },
    selectFileTempList() {
      if (_.isEmpty(this.temps)) {
        this.loadFileTemp(() => {
          this.showTempList(this.temps)
        })
        return
      }
      this.showTempList(this.temps)
    },
    showTempList(list) {
      const h = this.$createElement;

      let child = [this.$tt('label_empty_file_list')]
      if (list.length > 0) {
        child = list.map(it => {
          return h('div', { class: 'mg_v1' }, [
            h('a', {
              class: 'a_file_link',
              attrs: {
                href: this.$ex_default_download_url + it.filePath,
                target: '_blank',
                download: it.fileName,
              }
            }, [h('i', { class: 'el-icon-download el-icon--left' }), it.fileName])
          ])
        })
      }
      this.$msgbox({
        // title: this.$tt('label_file_list'),
        title: this.label,
        message: h('div', null, child
        ),
        showConfirmButton: false,
      }).then(action => {

      });
    },
  },
}
</script>

